package com.example.springboot_vue.controller;


import com.example.springboot_vue.common.CommonDto;
import com.example.springboot_vue.common.PageDto;
import com.example.springboot_vue.entity.StorageEntity;
import com.example.springboot_vue.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sgp
 * @since 2024-07-19
 */
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    private StorageService storageService;

    @GetMapping("/selectAll")
    public CommonDto selectAll(com.example.springboot_vue.entity.StorageEntity storageEntity){
        CommonDto<PageDto<com.example.springboot_vue.entity.StorageEntity>> commonDto = new CommonDto<>();
        PageDto<com.example.springboot_vue.entity.StorageEntity> pageDto = storageService.selectAll(storageEntity);
        commonDto.setContent(pageDto);
        return commonDto;
    }

    @GetMapping("/del/{id}")
    public CommonDto del(@PathVariable Integer id){
        CommonDto<com.example.springboot_vue.entity.StorageEntity> commonDto = new CommonDto<>();
        storageService.removeById(id);
        commonDto.setMessage("删除成功");
        return commonDto;
    }

    @PostMapping("/save")
    public CommonDto save(@RequestBody com.example.springboot_vue.entity.StorageEntity storageEntity){
        CommonDto<com.example.springboot_vue.entity.StorageEntity> commonDto = new CommonDto<>();
        storageService.save(storageEntity);
        commonDto.setMessage("新增成功");
        return commonDto;
    }

    @PostMapping("/update")
    public CommonDto update(@RequestBody com.example.springboot_vue.entity.StorageEntity storageEntity){
        CommonDto<com.example.springboot_vue.entity.StorageEntity> commonDto = new CommonDto<>();
        storageService.updateById(storageEntity);
        commonDto.setMessage("修改成功");
        return commonDto;
    }

//    查询数据库表中的所有记录
    @GetMapping("/list")
    public CommonDto list(){
        CommonDto<List<StorageEntity>> commonDto = new CommonDto<>();
        List<StorageEntity> list = storageService.list();
        commonDto.setContent(list);
        return commonDto;
    }
}
